package com.cloud.magic_api.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.magic_api.entity.SysDatabase;
import com.cloud.magic_api.service.ISysDatabaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 数据库配置信息
 *
 * @author yzj
 * @since 2022-08-25
 */
@Tag(name = "数据库配置信息")
@RestController
@RequestMapping("/magicApi/sysDatabase")
public class SysDatabaseController extends BaseController {

    @Autowired
    private ISysDatabaseService sysDatabaseService;

    @Operation(summary = "数据库配置信息分页")
    @GetMapping("/list")
    public Result list(SysDatabase sysDatabase) {
        startPage();
        List<SysDatabase> list = sysDatabaseService.selectSysDatabaseList(sysDatabase);
        return getDataTable(list);
    }

    @Operation(summary = "新增数据库配置信息")
    @Logs(title = "新增数据库配置", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public Result insert(@RequestBody SysDatabase sysDatabase) {
        return sysDatabaseService.insertObject(sysDatabase);
    }

    @Operation(summary = "修改数据库配置信息")
    @Logs(title = "修改数据库配置", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody SysDatabase sysDatabase) {
        return sysDatabaseService.updateObject(sysDatabase);
    }

    @Operation(summary = "查询数据库配置详情")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysDatabaseService.getById(id));
    }

    @Operation(summary = "删除数据库配置信息")
    @Logs(title = "删除数据库配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(sysDatabaseService.removeByIds(list));
    }

    @Operation(summary = "刷新数据源信息")
    @GetMapping("/refreshDatabase")
    public Result refreshDatabase(String dataName) {
        return sysDatabaseService.refreshDatabase(dataName);
    }

}
